using Gateway.Common.Enums;
using Gateway.IApplication.DescribeDomain;
using Gateway.IApplication.DescribeDomain.Input;
using Aurora.SingalR.Hubs;
using Aurora.TaskScheduler.Abstractions;
using Aurora.TaskScheduler.Abstractions.BaseModel;

namespace Gateway.UI.Api.TaskJobs;

/// <summary>
/// 
/// </summary>
public class DescribeDomainSaveJob : ITaskJob<Core.Ali.DomainRequest>
{
    private readonly IDescribeDomainApp _describeDomainApp;
    private readonly HubHandlerProvider _hubHandlerProvider;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="describeDomainApp"></param>
    /// <param name="hubHandlerProvider"></param>
    public DescribeDomainSaveJob(IDescribeDomainApp describeDomainApp, HubHandlerProvider hubHandlerProvider)
    {
        _describeDomainApp = describeDomainApp;
        _hubHandlerProvider = hubHandlerProvider;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="parameter"></param>
    /// <returns></returns>
    public async Task<JobResult> ExecuteAsync(Core.Ali.DomainRequest parameter)
    {
        Core.Ali.DomainRequest request = parameter;
        int page = 1;
        int size = 100;
        bool next = true;
        List<CreateDescribeDomainInput> commands = new List<CreateDescribeDomainInput>();
        while (next)
        {
            request.Page = page;
            request.PageSize = size;
            var tmpData = Core.Ali.DescribeDomains.GetList(request);
            if (tmpData.Body is { TotalCount: > 0 } &&
                tmpData.Body.Domains.Domain.Count > 0)
            {
                foreach (var item in tmpData.Body.Domains.Domain)
                {
                    var command = new CreateDescribeDomainInput()
                    {
                        DomainName = item.DomainName,
                        DomainId = item.DomainId,
                        AliDomain = item.AliDomain ?? false,
                        InstanceEndTime = item.InstanceEndTime,
                        InstanceExpired = item.InstanceExpired ?? false,
                        DnsServers = (item.DnsServers?.DnsServer?.Count > 0)
                            ? string.Join(",", item.DnsServers.DnsServer)
                            : "",
                        Endpoint = request.Endpoint
                    };
                    command.Records = GetData(item.DomainName, request);
                    commands.Add(command);
                    await _describeDomainApp.Save(command);
                }
            }
            else
            {
                next = false;
            }

            page++;
        }

        await _hubHandlerProvider.Send(new HubMessage()
        {
            Type = SendTypeEnum.Person.Value,
            Message = "转存成功",
            User = request.UserId,
            Creator = request.UserId
        });
        return new JobResult()
        {
            Success = true,
            Message = "转存执行成功"
        };
    }

    private List<DescribeDomainRecordInput> GetData(string domainName, Core.Ali.DomainRequest request)
    {
        int page = 1;
        int size = 100;
        bool next = true;
        request.DomainName = domainName;
        List<DescribeDomainRecordInput> commands = new List<DescribeDomainRecordInput>();
        while (next)
        {
            request.Page = page;
            request.PageSize = size;
            var tmpData = Core.Ali.DescribeDomains.GetRecordList(request);
            if (tmpData != null && tmpData.Body != null && tmpData.Body.TotalCount > 0 &&
                tmpData.Body.DomainRecords.Record.Count > 0)
            {
                foreach (var item in tmpData.Body.DomainRecords.Record)
                {
                    var command = new DescribeDomainRecordInput()
                    {
                        DomainName = domainName,
                        Status = item.Status,
                        Type = item.Type,
                        RecordId = item.RecordId,
                        Remark = item.Remark,
                        TTL = item.TTL ?? 0,
                        RR = item.RR,
                        Weight = item.Weight ?? 0,
                        Value = item.Value,
                        Endpoint = request.Endpoint
                    };
                    commands.Add(command);
                }
            }
            else
            {
                next = false;
            }

            page++;
        }

        return commands;
    }
}